package com.xuanting.xuanting

import android.content.Context
import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import android.view.View

/**
 * Created by lijianbin on 2018/12/5.
 */

class MyCoordinatorLayout : CoordinatorLayout {

    constructor(context: Context) : super(context) {}
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int) {
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type)
        if (dyConsumed > 0) {
            //悬浮状态
            titleChangeListener?.newTitle()

        } else if (dyConsumed == 0) {
            //非悬浮状态
            titleChangeListener?.oldTitle()
        }
    }

    override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int) {
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
        if (dyConsumed > 0) {
            titleChangeListener?.newTitle()
        } else if (dyConsumed == 0) {
            titleChangeListener?.oldTitle()
        }

    }

    private var titleChangeListener: TitleChangeListener? = null

    interface TitleChangeListener {
        fun newTitle()
        fun oldTitle()
    }

    fun setTitleListener(l: TitleChangeListener) {
        this.titleChangeListener = l
    }
}
